package handle;

import java.util.List;

import msg.CreateMsg;
import msg.DeleteMsg;
import data.Node;
import data.Tree;

public class DeleteMsgHandler {
	private static Tree tree = new Tree();

	public static void hander(DeleteMsg msg) throws Exception {
		String path = new String(msg.getPath());
		int index = path.lastIndexOf("/");
		String pPath = path.substring(0, index);
		Node node = tree.get(path);
		if (node == null) {
			throw new Exception();
		}
		tree.delete(path);

		Node pNode = tree.get(pPath);
		if (pNode == null) {
			throw new Exception();
		}
		List<Node> subNodes = pNode.getSubNodes();
		for (int i = 0; i < subNodes.size(); i++) {
			Node tnode = subNodes.get(i);
			if (tnode.getPath().equals(path)) {
				subNodes.remove(i);
			}
		}
	}

	public static void main(String[] args) throws Exception {
		/*
		 * MsgHandler.hander((new CreatePathMsg("/root/a", "是世界上最流行的脚本")).encode());
		 * MsgHandler.hander((new CreatePathMsg("/root/a/b", "是世界上最流行的脚本")).encode());
		 * MsgHandler.hander((new CreatePathMsg("/root/a/b/c", "是世界上最流行的脚本")).encode());
		 * MsgHandler.hander((new CreatePathMsg("/root/a/b/c/d", "是世界上最流行的脚本")).encode());
		 * MsgHandler.hander((new CreatePathMsg("/root/a/b/c/d/e", "是世界上最流行的脚本")).encode());
		 * MsgHandler.hander((new CreatePathMsg("/root/a/b/c", "是世界上最流行的脚本")).encode());
		 * MsgHandler.hander((new CreatePathMsg("/root/a/b/c/d", "是世界上最流行的脚本")).encode());
		 * tree.snap();
		 */
		tree.init();
		MsgHandler.hander((new CreateMsg("/root/a/b/c/d", "是世界上最流行的脚本")).encode());

		System.out.println();
	}
}